package com.javaex.book.services.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;

import com.javaex.book.mapper.OrderItemMapper;
import com.javaex.book.mapper.OrderMapper;
import com.javaex.book.mapper.UserMapper;
import com.javaex.book.pojo.Order;
import com.javaex.book.pojo.User;
import com.javaex.book.services.UserService;
import com.javaex.book.utils.ReturnResult;

@Service
public class UserServiceImpl implements UserService {

	private final UserMapper userMapper;
	private final OrderMapper orderMapper;
	private final OrderItemMapper orderItemMapper;
	

	@Autowired
	public UserServiceImpl(UserMapper userMapper, OrderMapper orderMapper, OrderItemMapper orderItemMapper) {
		this.userMapper = userMapper;
		this.orderMapper = orderMapper;
		this.orderItemMapper = orderItemMapper;
	}

	@Override
	public ReturnResult login(String username, String password) {
		if (username == null || password == null) {
			return ReturnResult.fail("登录失败");
		}
		// 根据用户名查询用户信息
		User user = userMapper.selectUserByUsernameOrEmail(username, null);
		if (user == null) {
			return ReturnResult.fail("用户不存在");
		}
		// 对密码进行md5加密，然后比较密码
		password = DigestUtils.md5DigestAsHex(password.getBytes());
		if (!password.equals(user.getPassword())) {
			return ReturnResult.fail("密码错误");
		}
		return ReturnResult.success("登录成功", user);
	}

	@Override
	public ReturnResult register(User user) {
		// 根据用户名和邮箱查询用户信息，判断该用户是否存在
		User userData = userMapper.selectUserByUsernameOrEmail(user.getUsername(), user.getEmail());
		if (userData != null) {
			return ReturnResult.fail("用户名或邮箱已被注册");
		}
		// 密码md5加密，然后保存用户信息
		user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		Integer rows = userMapper.insertUser(user);
		if (rows == 0) {
			return ReturnResult.fail("注册失败");
		}
		return ReturnResult.success("注册成功");
	}

	@Override
	public ReturnResult updateUser(User user) {
		// 如果要修改密码，将密码md5加密
		if (user.getPassword() != null && !user.getPassword().isEmpty()) {
			user.setPassword(DigestUtils.md5DigestAsHex(user.getPassword().getBytes()));
		}
		Integer rows = userMapper.updateUser(user);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("修改失败");
		}
		// 修改成功，将修改后的用户信息查询出来
		return ReturnResult.success("修改成功");
	}

	@Override
	public List<User> getUserList() {
		return userMapper.selectUserList();
	}

	@Override
	public User getUserById(Integer id) {
		return userMapper.selectUserById(id);
	}

	@Transactional
	@Override
	public ReturnResult deleteUser(Integer id) {
		// 删除用户
		Integer rows = userMapper.deleteUser(id);
		if (rows == null || rows == 0) {
			return ReturnResult.fail("删除失败");
		}
		// 查询出该用户的所有订单
		List<Order> orderList = orderMapper.selectOrderList(id, null, null);
		// 删除所有订单项
		for (Order order : orderList) {
			orderItemMapper.deleteOrderItem(order.getId());
		}
		// 删除该用户的所有订单
		orderMapper.deleteOrderByUserId(id);
		return ReturnResult.success("删除成功");
	}
}
